<template>
  <el-dialog
    class="createTask"
    title="新增任务"
    :visible="visible"
    :before-close="handleClose"
    :width="handleScreenWidthToVw(574)+'px'"
  >
    <div class="justify-center">
      <el-form :model="formData" :rules="rules" label-position="right" label-suffix="：" label-width="95px" ref="form">
        <el-form-item label="所属算法">{{ algAbility.algAbilityName }}</el-form-item>
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="formData.name" clearable placeholder="请输入任务名称"></el-input>
        </el-form-item>
        <el-form-item label="供应商" prop="algOriginEnum">
          <el-select v-model="formData.algOriginEnum" clearable>
            <el-option
              v-for="({label,value},index) in algOrigin"
              :key="index"
              :value="value"
              :label="label"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit">提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { handleScreenWidthToVw } from '@/utils/tools'
import { AlgOriginEnum } from '@/views/algorithm/consts'
import { apiAddAlgTask } from '@/views/algorithm/alg-market/api'

export default {
  name: 'CreateTaskDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    algAbility: {
      type: Object,
      required: true,
      default: () => ({
        algAbilityId: '',
        algAbilityName: ''
      })
    }
  },
  data() {
    return {
      formData: {
        algAbilityId: '',
        algAbilityName: '',
        algOriginEnum: '',
        name: ''
      }
    }
  },
  watch: {
    algAbility(value) {
      this.formData = {
        ...this.formData,
        ...value
      }
    }
  },

  computed: {
    handleScreenWidthToVw: () => handleScreenWidthToVw,
    algOrigin() {
      const toList = []
      for (const [key, value] of AlgOriginEnum) {
        toList.push({
          label: value,
          value: key
        })
      }
      return toList
    },
    rules() {
      return {
        algOriginEnum: [{ required: true, message: '算法供应商不能为空', trigger: 'change' }],
        algTaskName: [{ required: true, message: '算法名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          apiAddAlgTask(this.formData)
            .then(() => {
              this.$message.success('新增任务成功')
              this.$emit('change')
            })
            .finally(() => {
              this.handleClose()
            })
        }
      })
    }

  }
}
</script>

<style scoped lang="scss">
.createTask {
}
</style>
